package 创建型设计模式.原型模式.demo;

/**
 * @Author lf
 * @Date 3/29/2024
 */
public class PrototypeDemo {
    public static void main(String[] args) throws CloneNotSupportedException {
        ConcretePrototype original = new ConcretePrototype("original");
        ConcretePrototype cloned = (ConcretePrototype) original.clone();

        System.out.println(original.getField()); // 输出: original
        System.out.println(cloned.getField());   // 输出: original

        // 深拷贝示例
        ComplexObject co = new ComplexObject("complex property");
        DeepPrototype originalDeep = new DeepPrototype(co);
        DeepPrototype clonedDeep = (DeepPrototype) originalDeep.clone();

        System.out.println(originalDeep == clonedDeep);                           // 输出: false
        System.out.println(originalDeep.clone().getClass() == clonedDeep.getClass()); // 输出: true
        System.out.println(originalDeep.clone() == clonedDeep.clone());              // 输出: false
    }
}
